import { Injectable } from '@angular/core';
import { QueryOptions } from '../../model/common/query-options';
import { Observable } from 'rxjs';
import { ApiPagedData } from '../../model/common/api-paged-data';
import { UserDTO } from '../../model/security/user-dto';
import { _HttpClient } from '@delon/theme';
import { ChangePasswordInfoDTO } from '../../model/security/change-password-info-dto';

@Injectable({
  providedIn: 'root',
})
export class UserService {
  /**
   * API的入口
   */
  private static URL = '/api/icm-user';

  constructor(private http: _HttpClient) {}

  /**
   * 分页查询用户信息
   * @param option 分页参数
   * @param username 用户名
   * @param role 用户角色
   */
  findOnePage(option: QueryOptions, username?: string, role?: string): Observable<ApiPagedData<UserDTO>> {
    const params = {};
    Object.assign(params, option, username ? { username } : {}, role ? { role } : {});
    return this.http.get<ApiPagedData<UserDTO>>(UserService.URL, params);
  }

  /**
   * 通过id查找用户
   */
  findById(id: string): Observable<UserDTO> {
    return this.http.get<UserDTO>(`${UserService.URL}/${id}`);
  }

  /**
   * 修改用户密码
   */
  updateUserPassword(id: string, changePasswordInfoDTO: ChangePasswordInfoDTO): Observable<UserDTO> {
    return this.http.put<UserDTO>(`${UserService.URL}/updateUserPassword/${id}`, changePasswordInfoDTO);
  }

  /**
   * 更新用户信息
   * @param id 用户id
   * @param userDTO 用户
   */
  update(id: string, userDTO: UserDTO): Observable<UserDTO> {
    return this.http.put<UserDTO>(`${UserService.URL}/${id}`, userDTO);
  }

  /**
   * 根据id删除用户
   * @param id 用户id
   */
  delete(id: string): Observable<UserDTO> {
    return this.http.delete<UserDTO>(`${UserService.URL}/${id}`);
  }
}
